package com.xuelang.pipeline.domain.application.service.impl;

import com.xuelang.pipeline.client.application.dto.ComponentQuery;
import com.xuelang.pipeline.client.common.constant.Constant;
import com.xuelang.pipeline.client.common.exception.BizException;
import com.xuelang.pipeline.client.common.result.PageRecord;
import com.xuelang.pipeline.domain.application.Component;
import com.xuelang.pipeline.domain.application.repository.ComponentRepository;
import com.xuelang.pipeline.domain.application.service.ComponentDomainService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.concurrent.ThreadLocalRandom;

/**
 * 组件领域服务实现类
 *
 * @author xiwen.zxg
 * @since 2024/5/15
 */
@Service
public class ComponentDomainServiceImpl implements ComponentDomainService {

    @Autowired
    private ComponentRepository componentRepository;

    @Override
    public PageRecord<Component> pageList(long currentPage, long pageSize, ComponentQuery componentQuery) {
        return componentRepository.pageList(currentPage, pageSize, componentQuery);
    }

    @Override
    public List<Component> list(ComponentQuery componentQuery) {
        return componentRepository.list(componentQuery);
    }

    @Override
    public Component save(Component component) {
        if(existsCode(component.getApplicationCode(), component.getCode())) {
            throw new BizException("组件编码已经存在");
        }
        if(existsName(component.getApplicationCode(), component.getCode(), component.getName())) {
            throw new BizException("组件名称已经存在");
        }
        String uniqueLabel = generateGlobalUniqueLabel();
        while(existsLabel(uniqueLabel)) {
            uniqueLabel = generateGlobalUniqueLabel();
        }
        component.setLabel(component.getCode() + Constant.SEPARATOR_MIDDLE_LINE + uniqueLabel);
        if (componentRepository.save(component)) {
            return component;
        }
        return null;
    }

    @Override
    public Boolean update(Component component) {
        // 更新时，名称不能重复，摒弃当前组件的名称。
        if(existsName(component.getApplicationCode(), component.getCode(), component.getName())) {
            throw new BizException("name已经存在");
        }
        return componentRepository.update(component);
    }

    @Override
    public Boolean delete(String componentLabel) {
        return componentRepository.delete(componentLabel);
    }

    @Override
    public Component query(String componentLabel) {
        return componentRepository.query(componentLabel);
    }

    @Override
    public Boolean existsCode(String applicationCode, String componentCode) {
        return componentRepository.existsCode(applicationCode, componentCode);
    }

    @Override
    public Boolean existsLabel(String uniqueLabel) {
        return componentRepository.existsLabel(uniqueLabel);
    }

    @Override
    public Boolean existsName(String applicationCode, String componentCode, String componentName) {
        return componentRepository.existsName(applicationCode, componentCode, componentName);
    }

    @Override
    public String generateGlobalUniqueLabel() {
        StringBuilder plateNumber = new StringBuilder();
        int index = 0;
        while (index < Constant.UNIQUE_LABEL_LENGTH) {
            plateNumber.append(
                    Constant.PLATE_NUMBER_CHARACTER
                    .charAt(ThreadLocalRandom.current().nextInt(Constant.PLATE_NUMBER_CHARACTER_LENGTH))
            );
            index++;
        }
        return plateNumber.toString();
    }

    @Override
    public Component queryById(Long id) {

        return componentRepository.queryById(id);
    }
}
